<?php

namespace catchAdmin\basics\model;

use catchAdmin\basics\model\search\ProviderSearch;
use catcher\base\CatchModel;
use think\facade\Db;
use think\facade\Log;

/**
 *
 * @property int $id
 * @property string $account_code
 * @property string $account_name
 * @property string $account_key
 * @property string $addr
 * @property string $sell
 * @property string $remark
 * @property string $role
 * @property int $tenant_id
 * @property int $creator_id
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class Provider extends CatchModel
{
    use ProviderSearch;
    
    public $field = [
        //
        'id',
        // 账户代码
        'account_code',
        // 账户名称
        'account_name',
        // 账户简称
        'account_key',
        // 地址
        'addr',
        // 联系人
        'relation_name',
        // 联系电话
        'relation_phone',
        // 备注
        'remark',
        // 角色
        'role',
        //
        'tenant_id',
        //
        'creator_id',
        //
        'created_at',
        //
        'updated_at',
        //
        'deleted_at',
        //
        'tax',
        //
        'bank_name',
        //
        'bank_code',
        'serial_number',
        'usci',
        'tax_id',
        'settlement',//结算方
        'country',
        'prov',
        'city',
        'zip_code'
    ];
    
    public $name = 'db_provider';
    /**
     * 列表
     * @throws \think\db\exception\DbException
     * @return \think\Paginator
     */
    public function getList($tenant_id = 0)
    {
        $where = [];
        if($tenant_id != 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        return $this->catchSearch()
            ->order('id','desc')
            ->where($where)
            ->withoutField('tenant_id,creator_id,updated_at,deleted_at,created_at')
            ->paginate()
            ->each(function (&$item){
                $item['part_name'] = implode(',',Db::table('db_part')
                    ->whereIn('id',$item['role'])
                    ->where('deleted_at','=',0)
                    ->column('part_name'));
                $item['role'] = Db::table('db_part')
                    ->whereIn('id',$item['role'])
                    ->where('deleted_at','=',0)
                    ->column('id');
            });
    }

    public static function onAfterInsert($provider){
        $provider->serial_number = "C" . str_pad($provider->id,5,'0',STR_PAD_LEFT);
        $provider->save();
    }

    //委托单位
    public function getEntrust($tenant_id=0){
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }

        $field = "*";

        $result =  $this
            ->catchSearch()
//            ->field($field)
            ->where($where)
            ->withoutField('tenant_id,creator_id,updated_at,deleted_at,created_at')
            ->order($this->aliasField('id'), 'desc')
            ->select()->toArray();

        foreach ($result as $key => &$value){
            if(!in_array(10,explode(',',$value['role']))){
                unset($result[$key]);
            }
        }

        return array_values($result);
    }
}